
import {
    Block,
    Hash,
    Transaction,
    TransactionReceipt,
    createTestClient,
    publicActions,
    walletActions,
    webSocket,
  } from "viem";
  import { hardhat } from "viem/chains";
  import { TransactionWithFunction } from "./utils/scaffold-eth/block";
  import { GenericContractsDeclaration } from "./utils/scaffold-eth/contract";
import { Abi, decodeFunctionData, getAbiItem } from "viem";
//import contractData from "./contracts/deployedContracts";
  //import { decodeTransactionData } from "./utils/scaffold-eth/decodeTxData";

  const contractData = {
    31337: {
      BuyMeACoffee: {
        address: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
        abi: [
          {
            inputs: [],
            stateMutability: "nonpayable",
            type: "constructor",
          },
          {
            inputs: [],
            name: "InsufficientFunds",
            type: "error",
          },
          {
            inputs: [
              {
                internalType: "string",
                name: "message",
                type: "string",
              },
            ],
            name: "InvalidArguments",
            type: "error",
          },
          {
            inputs: [],
            name: "OnlyOwner",
            type: "error",
          },
          {
            anonymous: false,
            inputs: [
              {
                indexed: true,
                internalType: "address",
                name: "buyer",
                type: "address",
              },
              {
                indexed: false,
                internalType: "uint256",
                name: "price",
                type: "uint256",
              },
            ],
            name: "BuyMeACoffeeEvent",
            type: "event",
          },
          {
            anonymous: false,
            inputs: [
              {
                indexed: true,
                internalType: "address",
                name: "userAddress",
                type: "address",
              },
              {
                indexed: false,
                internalType: "uint256",
                name: "time",
                type: "uint256",
              },
              {
                indexed: false,
                internalType: "uint256",
                name: "numCoffees",
                type: "uint256",
              },
              {
                indexed: false,
                internalType: "string",
                name: "userName",
                type: "string",
              },
              {
                indexed: false,
                internalType: "string",
                name: "twitterHandle",
                type: "string",
              },
              {
                indexed: false,
                internalType: "string",
                name: "message",
                type: "string",
              },
            ],
            name: "NewMemo",
            type: "event",
          },
          {
            inputs: [
              {
                internalType: "uint256",
                name: "numCoffees",
                type: "uint256",
              },
              {
                internalType: "string",
                name: "userName",
                type: "string",
              },
              {
                internalType: "string",
                name: "twitterHandle",
                type: "string",
              },
              {
                internalType: "string",
                name: "message",
                type: "string",
              },
            ],
            name: "buyCoffee",
            outputs: [],
            stateMutability: "payable",
            type: "function",
          },
          {
            inputs: [],
            name: "getMemos",
            outputs: [
              {
                components: [
                  {
                    internalType: "uint256",
                    name: "numCoffees",
                    type: "uint256",
                  },
                  {
                    internalType: "string",
                    name: "userName",
                    type: "string",
                  },
                  {
                    internalType: "string",
                    name: "twitterHandle",
                    type: "string",
                  },
                  {
                    internalType: "string",
                    name: "message",
                    type: "string",
                  },
                  {
                    internalType: "uint256",
                    name: "time",
                    type: "uint256",
                  },
                  {
                    internalType: "address",
                    name: "userAddress",
                    type: "address",
                  },
                ],
                internalType: "struct Memo[]",
                name: "",
                type: "tuple[]",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [
              {
                internalType: "uint256",
                name: "",
                type: "uint256",
              },
            ],
            name: "memos",
            outputs: [
              {
                internalType: "uint256",
                name: "numCoffees",
                type: "uint256",
              },
              {
                internalType: "string",
                name: "userName",
                type: "string",
              },
              {
                internalType: "string",
                name: "twitterHandle",
                type: "string",
              },
              {
                internalType: "string",
                name: "message",
                type: "string",
              },
              {
                internalType: "uint256",
                name: "time",
                type: "uint256",
              },
              {
                internalType: "address",
                name: "userAddress",
                type: "address",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [
              {
                internalType: "uint256",
                name: "index",
                type: "uint256",
              },
              {
                internalType: "string",
                name: "message",
                type: "string",
              },
            ],
            name: "modifyMemoMessage",
            outputs: [],
            stateMutability: "nonpayable",
            type: "function",
          },
          {
            inputs: [],
            name: "owner",
            outputs: [
              {
                internalType: "address payable",
                name: "",
                type: "address",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [],
            name: "price",
            outputs: [
              {
                internalType: "uint256",
                name: "",
                type: "uint256",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [
              {
                internalType: "uint256",
                name: "index",
                type: "uint256",
              },
            ],
            name: "removeMemo",
            outputs: [],
            stateMutability: "nonpayable",
            type: "function",
          },
          {
            inputs: [],
            name: "withdrawTips",
            outputs: [],
            stateMutability: "nonpayable",
            type: "function",
          },
          {
            stateMutability: "payable",
            type: "receive",
          },
        ],
        inheritedFunctions: {},
      },
      YourContract: {
        address: "0x5FbDB2315678afecb367f032d93F642f64180aa3",
        abi: [
          {
            inputs: [
              {
                internalType: "address",
                name: "_owner",
                type: "address",
              },
            ],
            stateMutability: "nonpayable",
            type: "constructor",
          },
          {
            anonymous: false,
            inputs: [
              {
                indexed: true,
                internalType: "address",
                name: "greetingSetter",
                type: "address",
              },
              {
                indexed: false,
                internalType: "string",
                name: "newGreeting",
                type: "string",
              },
              {
                indexed: false,
                internalType: "bool",
                name: "premium",
                type: "bool",
              },
              {
                indexed: false,
                internalType: "uint256",
                name: "value",
                type: "uint256",
              },
            ],
            name: "GreetingChange",
            type: "event",
          },
          {
            inputs: [],
            name: "greeting",
            outputs: [
              {
                internalType: "string",
                name: "",
                type: "string",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [],
            name: "owner",
            outputs: [
              {
                internalType: "address",
                name: "",
                type: "address",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [],
            name: "premium",
            outputs: [
              {
                internalType: "bool",
                name: "",
                type: "bool",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [
              {
                internalType: "string",
                name: "_newGreeting",
                type: "string",
              },
            ],
            name: "setGreeting",
            outputs: [],
            stateMutability: "payable",
            type: "function",
          },
          {
            inputs: [],
            name: "totalCounter",
            outputs: [
              {
                internalType: "uint256",
                name: "",
                type: "uint256",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [
              {
                internalType: "address",
                name: "",
                type: "address",
              },
            ],
            name: "userGreetingCounter",
            outputs: [
              {
                internalType: "uint256",
                name: "",
                type: "uint256",
              },
            ],
            stateMutability: "view",
            type: "function",
          },
          {
            inputs: [],
            name: "withdraw",
            outputs: [],
            stateMutability: "nonpayable",
            type: "function",
          },
          {
            stateMutability: "payable",
            type: "receive",
          },
        ],
        inheritedFunctions: {},
      },
    },
  };

  type ContractsInterfaces = Record<string, Abi>;
  type TransactionType = TransactionWithFunction | null;
  
  const deployedContracts = contractData as GenericContractsDeclaration | null;

  console.log("deployedContracts =  ")
  console.log(JSON.stringify(deployedContracts))

  const chainMetaData = deployedContracts?.[hardhat.id];

  console.log("chainMetaData =") 
  console.log(JSON.stringify(chainMetaData))


  const interfaces = chainMetaData
    ? Object.entries(chainMetaData).reduce((finalInterfacesObj, [contractName, contract]) => {
        finalInterfacesObj[contractName] = contract.abi;
        return finalInterfacesObj;
      }, {} as ContractsInterfaces)
    : {};
    
    console.log("interfaces =")
    console.log(JSON.stringify(interfaces))  

  